package com.next.muscu;

import java.util.HashMap;
import java.util.Map;
import java.util.prefs.Preferences;

import javax.swing.Icon;
import javax.swing.ImageIcon;

import com.next.muscu.visual.MainScreen;


public class Application {

	private static Application appl = null;
	public static final Integer IMG_ACCEPT = new Integer(0);
	public static final Integer IMG_ADD = new Integer(1);
	public static final Integer IMG_REMOVE = new Integer(2);
	public static final Integer IMG_COMMENT_ADD = new Integer(3);
	public static final Integer IMG_COMMENT = new Integer(4);
	
	private String imagedir = "icon/";
	public static final String PP_PDFROOT = "pdfRoot";
	public static final String PP_PHOTOROOT = "photoRoot";
	
	private MissingIcon missingIcone = new MissingIcon();
	
	private String[] imageFileNames= {"accept.png","add.png","remove.png","comment_add.png","comment.png"};
	
	private String[] imageCaptions = {"ok","add","remove","comment_add","comment"};
	//TODO passer en mode system prefrence et pas user
	Preferences perf = Preferences.userNodeForPackage(Application.class);
	MainScreen  screen;
	Map<Integer,Icon> image = new HashMap<Integer, Icon>();
	UserMessageVector userMessageVector = new UserMessageVector();
	
	private Application()
	{
		//load img in background
		Loadimages loadimages = new Loadimages();
		loadimages.start();

                //Load the persistance Unit
                //TODO gestion des exceptions
                PersistanceUnitTools.getEntityManagerFactory();
		//before run wait for all thread
		try {
			loadimages.join();
			LunchGui l = new LunchGui();
			l.start();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} 
		
	};
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		appl = new Application();
		
	}
	public static Application getAppl()
	{
		return appl;
		
	}
	private void run()
	{
		

	}

	 /**
     * Cr�e une image si le chemin est valide
     * @param String - resource path
     * @param String - description de l'image
     */
    protected ImageIcon createImageIcon(String path,
            String description) {
        java.net.URL imgURL = getClass().getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL, description);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
         
        }
        
    private class Loadimages extends Thread {   
  
        @Override
        public void run() {
            for (int i = 0; i < imageCaptions.length; i++) {
                ImageIcon icon;
                icon = createImageIcon(imagedir + imageFileNames[i], imageCaptions[i]);
                
                if(icon != null){
                    image.put(i, icon);  
                }else{
                	image.put(i, missingIcone);
                }
            }
        }
    };
    private class LunchGui extends Thread {   
    	  
        @Override
        public void run() {screen = new MainScreen(); 
        }
        	
        }
    public String getPdfRoot()
    {
    	return perf.get(PP_PDFROOT, null);
    }
    public void setPdfRoot(String s)
    {
    	
    	perf.put(Application.PP_PDFROOT, s);
    }
    public Map<Integer,Icon> getImage()
    {return image;}
    
    public Icon getImage(Integer i)
    {return image.get(i);}
    
    public UserMessageVector getUserMessages()
    {
    	return userMessageVector;
    }
	public String getPhotoRoot(String path) {
		return perf.get(PP_PHOTOROOT, null);
	}
	 public void setPhotoRoot(String s)
	 {
	    	perf.put(Application.PP_PHOTOROOT, s);
	 }
}